package com.xiaoshuidi.cloud.module.contract.mq.consumer;

import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.framework.systemevent.core.LeaseSystemEventData;
import com.xiaoshuidi.cloud.framework.systemevent.core.LeaseSystemEventListener;
import com.xiaoshuidi.cloud.framework.systemevent.core.LeaseSystemEventTypeEnum;
import com.xiaoshuidi.cloud.framework.systemevent.core.data.LeaseSystemEventMobileModify;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.ContractRoomDO;
import com.xiaoshuidi.cloud.module.contract.service.bill.OrderFeesService;
import com.xiaoshuidi.cloud.module.contract.service.contract.ContractHouseOwnerService;
import com.xiaoshuidi.cloud.module.contract.service.contract.ContractRoomService;
import com.xiaoshuidi.cloud.module.contract.service.roommate.ContractRoommateService;
import com.xiaoshuidi.cloud.module.rooms.api.roomhouse.RoomHouseApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static cn.hutool.core.util.ObjectUtil.isEmpty;

@Component
@Slf4j
public class MobileModifyListener implements LeaseSystemEventListener {
    @Resource
    private ContractRoomService contractRoomService;

    @Resource
    private OrderFeesService orderFeesService;

    @Resource
    private ContractRoommateService contractRoommateService;

    @Resource
    private ContractHouseOwnerService contractHouseOwnerService;

    @Resource
    private TransactionDefinition transactionDefinition;

    @Resource
    private PlatformTransactionManager transactionManager;

    @Resource
    private RoomHouseApi roomHouseApi;


    @Override
    public LeaseSystemEventTypeEnum getEventType() {
        return LeaseSystemEventTypeEnum.MOBILE_MODIFY;
    }

    @Override
    public void onEvent(LeaseSystemEventData event) {
        LeaseSystemEventMobileModify data = (LeaseSystemEventMobileModify) event;
        log.info("收到修改手机消息 {}#######################", JsonUtils.toJsonString(data));
        List<Long> roomIds = new ArrayList<>();
        TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);
        try {
            if (isEmpty(data.getUserId())) {

                log.info("新用户id为空，ignore");
            } else {
                //获取所有的roomIds
                List<ContractRoomDO> list = contractRoomService.getRoomIdsByMemberId(data.getOldUserId());
                if (list != null && list.size() > 0) {
                    roomIds = list.stream().map(ContractRoomDO::getRoomId).collect(Collectors.toList());
                    //1.contract_room
                    contractRoomService.updateMemberUser(data.getUserId(), data.getMobile(), data.getUserName(), data.getOldUserId());
                }
                //2.contract_rentorder
                orderFeesService.updateMemberUser(data.getMobile(), data.getUserName(), data.getOldMobile(), data.getUserId(), data.getOldUserId());
                //contract_roommate
                contractRoommateService.updateMemberUser(data.getUserId(), data.getMobile(), data.getUserName(), data.getOldUserId());
                //contract_house_owner
                contractHouseOwnerService.updateMemberUser(data.getUserId(), data.getMobile(), data.getUserName(), data.getOldUserId());
                transactionManager.commit(transactionStatus);
            }
        } catch (Exception e) {
            log.error("修改手机消息处理异常", e.getMessage());
            transactionManager.rollback(transactionStatus);
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), "修改手机消息处理异常!");
        }
        if (roomIds != null && roomIds.size() > 0) {
            log.info("修改es 房间号 {}#######################", JsonUtils.toJsonString(roomIds));
            roomHouseApi.updateESRoomByIds(roomIds);
            log.info("修改es 房间号完成 {}#######################");
        }

    }
}
